package cn.com.xinjinfu.pdoulcProduct.adapter;

import android.app.Activity;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.entity.MyInvestListBean;
import cn.com.xinjinfu.pdoulcProduct.entity.MyVoucherBean;
import cn.com.xinjinfu.pdoulcProduct.entity.MyVoucherBean.VoucherListA;
import cn.com.xinjinfu.pdoulcProduct.manager.L;
import cn.com.xinjinfu.pdoulcProduct.utils.TextViewUtil;

/**
 * Created by android-adt on 2016/3/31.
 */
public class MyVoucherListAdaper extends BaseAdapter {

    private LayoutInflater inflater;
    private List<VoucherListA> list;
    private VoucherListA termList;
    private int totalRows;

    public MyVoucherListAdaper(Activity context) {
        inflater = LayoutInflater.from(context);
        list = new ArrayList<>();
    }

    public void setList(List<VoucherListA> page) {
        list.clear();
        list.addAll(page);
        notifyDataSetChanged();
    }

    public void addList(List<VoucherListA> page){
        list.addAll(page);
        notifyDataSetChanged();
    }

    public void setTotalRows(int totalRows) {
        this.totalRows = totalRows;
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        //普通列表项
        if (convertView == null || convertView.getTag() == null) {
            convertView = inflater.inflate(R.layout.item_voucher_list, null);
            holder = new ViewHolder();
            holder.bg_item = (LinearLayout) convertView.findViewById(R.id.bg_item);
            holder.tv_money = (TextView) convertView.findViewById(R.id.tv_money);
            holder.tv_yuan = (TextView) convertView.findViewById(R.id.tv_yuan);
            holder.quan_type = (TextView) convertView.findViewById(R.id.quan_type);
            holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            holder.tv_infor = (TextView) convertView.findViewById(R.id.tv_infor);
            holder.tv_need_money = (TextView) convertView.findViewById(R.id.tv_need_money);
            holder.tv_app_pc = (TextView) convertView.findViewById(R.id.tv_app_pc);
            holder.tishi = (TextView) convertView.findViewById(R.id.tishi);
            holder.textView1 = (LinearLayout) convertView.findViewById(R.id.textView1);
            convertView.setTag(holder);//绑定ViewHolder对象
        } else {
            holder = (ViewHolder) convertView.getTag();//取出ViewHolder对象
        }

        termList = list.get(position);
        holder.quan_type.setText(termList.getTitle());
        if (termList.getType() ==1){
            holder.tv_money.setText(""+termList.getRate());
            holder.tv_yuan.setText("%");
        }else {
            holder.tv_money.setText(""+termList.getAmount());
            holder.tv_yuan.setText("元");
        }
        if (termList.getInvest_type() == 1){
            holder.tv_infor.setText("定期标的可用");
        }else if (termList.getInvest_type() == 2){
            holder.tv_infor.setText("转让标的可用");
        }else {
            holder.tv_infor.setText("定期、转让标的可用");
        }
        if (termList.getPlatform_type() == 1){
            holder.tv_app_pc.setText("APP");
        }else if (termList.getPlatform_type() == 2){
            holder.tv_app_pc.setText("PC");
        }else {
            holder.tv_app_pc.setText("APP/PC");
        }
        GradientDrawable myGrad = (GradientDrawable)holder.bg_item.getBackground();
        if (termList.getUseType() == 0){
            holder.tv_time.setText(termList.getReceice_time() + "-" + termList.getExpire_time());
            if (termList.getType() ==1){
                myGrad.setColor(0xFFF4B942);
            }else {
                myGrad.setColor(0xFF6ED3D6);
            }
        }else if (termList.getUseType() == 1){
            holder.tv_time.setText(termList.getUse_time());
            myGrad.setColor(0xFFCCCCCC);
        }else {
            holder.tv_time.setText(termList.getReceice_time() + "-" + termList.getExpire_time());
            myGrad.setColor(0xFFCCCCCC);
            //在失效兜券里面：“仅显示最近三个月兜券” 的提示；
            if(list.size() == totalRows && list.size() == position + 1){
                holder.tishi.setVisibility(View.VISIBLE);
            }else {
                holder.tishi.setVisibility(View.GONE);
            }
        }
        if (termList.getLimit_amount() > 0){
            holder.textView1.setVisibility(View.VISIBLE);
            holder.tv_need_money.setText("" + termList.getLimit_amount());
        }else {
            holder.textView1.setVisibility(View.INVISIBLE);
        }
        return convertView;
    }

    public final class ViewHolder {
        public LinearLayout bg_item;
        public TextView tv_money;
        public TextView tv_yuan;
        public TextView quan_type;
        public TextView tv_time;
        public TextView tv_infor;
        public TextView tv_need_money;
        public TextView tv_app_pc;
        public TextView tishi;
        public LinearLayout textView1;
    }
}
